﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB.Electrical;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;

namespace YArchitech.MEP.Electric.CableLay
{
	public partial class CableSystemType : System.Windows.Forms.Form
	{
		public CableSystemType(CableTray cableTray)
		{
			this.InitializeComponent();
			this.targetTray = cableTray;
		}

		private void DlgCableTraySystemType_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.xmlHelper.LoadXml(this.path, "Root");
			foreach (XmlElement xmlElement in new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData").GetElements("CableLayRule"))
			{
				string attribute = xmlElement.GetAttribute("Name");
				this.cmbSystemType.Items.Add(attribute);
			}
			this.LoadDlgData();
            string text = this.targetTray.get_Parameter(Autodesk.Revit.DB.BuiltInParameter.RBS_CTC_SERVICE_TYPE).AsString();
			if (!string.IsNullOrEmpty(text))
			{
				foreach (object obj in this.cmbSystemType.Items)
				{
					if (obj.ToString() == text)
					{
						this.cmbSystemType.SelectedItem = obj;
						break;
					}
				}
			}
			if (this.cmbSystemType.SelectedIndex == -1)
			{
				this.cmbSystemType.SelectedIndex = 0;
			}
		}

		private void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableSystemType", "Params"));
			string attribute = this.xmlHelper.GetAttribute("Index");
			if (!string.IsNullOrEmpty(attribute))
			{
				int num = Convert.ToInt32(attribute);
				if (this.cmbSystemType.Items.Count > num)
				{
					this.cmbSystemType.SelectedIndex = num;
				}
			}
			attribute = this.xmlHelper.GetAttribute("Applied");
			if (!string.IsNullOrEmpty(attribute))
			{
				if (attribute == "1")
				{
					this.chkApplied.Checked = true;
					return;
				}
				if (attribute == "0")
				{
					this.chkApplied.Checked = false;
				}
			}
		}

		private void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableSystemType"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableSystemType", "Params"));
			this.xmlHelper.SetAttribute("Index", this.cmbSystemType.SelectedIndex.ToString());
			if (this.chkApplied.Checked)
			{
				this.xmlHelper.SetAttribute("Applied", "1");
			}
			else
			{
				this.xmlHelper.SetAttribute("Applied", "0");
			}
			this.xmlHelper.SaveXml("");
		}

		private void DlgCableTraySystemType_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.OK;
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "CableSystemType.xml");

		private const string Root = "Root";

		private const string Node = "HYCableSystemType";

		private const string ChildNode = "Params";

		private const string CableTraySystemType = "Index";

		private const string Applied = "Applied";

		private List<string> cableTraySysTypes = new List<string>();

		private CableTray targetTray;
	}
}
